package com.xbts.system.service.impl;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xbts.common.exception.ServiceException;
import com.xbts.common.utils.StringUtils;
import com.xbts.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xbts.system.mapper.CnVirtuallyUserHwAccountMapper;
import com.xbts.system.domain.CnVirtuallyUserHwAccount;
import com.xbts.system.service.ICnVirtuallyUserHwAccountService;
import com.xbts.common.utils.DateUtils;
import com.xbts.common.utils.SecurityUtils;

/**
 * 用户和华为云账号绑定（虚拟）Service业务层处理
 * 
 * @author xbts
 * @date 2023-11-30
 */
@Service
public class CnVirtuallyUserHwAccountServiceImpl  extends ServiceImpl<CnVirtuallyUserHwAccountMapper, CnVirtuallyUserHwAccount> implements ICnVirtuallyUserHwAccountService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     * 查询用户和华为云账号绑定（虚拟）列表
     *
     * @param cnVirtuallyUserHwAccount 用户和华为云账号绑定（虚拟）
     * @return 用户和华为云账号绑定（虚拟）
     */
    @Override
    public List<CnVirtuallyUserHwAccount> selectCnVirtuallyUserHwAccountList(CnVirtuallyUserHwAccount cnVirtuallyUserHwAccount) {
        QueryWrapper<CnVirtuallyUserHwAccount> queryWrapper = cnVirtuallyUserHwAccount.queryWrapper();
        return list(queryWrapper);
    }

    /**
     * 查询用户和华为云账号绑定（虚拟）
     *
     * @param userId 用户和华为云账号绑定（虚拟）主键
     * @return 用户和华为云账号绑定（虚拟）
     */
    @Override
    public CnVirtuallyUserHwAccount selectCnVirtuallyUserHwAccountByUserId(String userId) {
        return getById(userId);
    }

    /**
     * 新增用户和华为云账号绑定（虚拟）
     *
     * @param cnVirtuallyUserHwAccount 用户和华为云账号绑定（虚拟）
     * @return 结果
     */
    @Override
    public boolean insertCnVirtuallyUserHwAccount(CnVirtuallyUserHwAccount cnVirtuallyUserHwAccount) {
        String hwAccount = cnVirtuallyUserHwAccount.getHwAccount();
        long count = count(new LambdaQueryWrapper<CnVirtuallyUserHwAccount>()
                .eq(CnVirtuallyUserHwAccount::getHwAccount, hwAccount));
        if(count > 0){
            throw new ServiceException("华为云账号已存在!");
        }
        String userName = cnVirtuallyUserHwAccount.getUserName();
        if(StringUtils.isNotBlank(userName)){
            long userNameCount = count(new LambdaQueryWrapper<CnVirtuallyUserHwAccount>()
                    .eq(CnVirtuallyUserHwAccount::getUserName, userName));
            if(userNameCount > 0){
                throw new ServiceException("用户账号已存在!");
            }
        }
        cnVirtuallyUserHwAccount.setCreateBy(SecurityUtils.getUserId().toString());
        return save(cnVirtuallyUserHwAccount);
    }

    /**
     * 修改用户和华为云账号绑定（虚拟）
     *
     * @param cnVirtuallyUserHwAccount 用户和华为云账号绑定（虚拟）
     * @return 结果
     */
    @Override
    public boolean updateCnVirtuallyUserHwAccount(CnVirtuallyUserHwAccount cnVirtuallyUserHwAccount) {
        CnVirtuallyUserHwAccount userHwAccount = getById(cnVirtuallyUserHwAccount.getUserId());
        if(StringUtils.isNotBlank(userHwAccount.getHwAccount()) && !userHwAccount.getHwAccount().equals(cnVirtuallyUserHwAccount.getHwAccount())){
            long count = count(new LambdaQueryWrapper<CnVirtuallyUserHwAccount>()
                   .eq(CnVirtuallyUserHwAccount::getHwAccount, cnVirtuallyUserHwAccount.getHwAccount()));
            if(count > 0){
                throw new ServiceException("华为云账号已存在!");
            }
        }
        if(StringUtils.isNotBlank(userHwAccount.getUserName()) && !userHwAccount.getUserName().equals(cnVirtuallyUserHwAccount.getUserName())){
            long userNameCount = count(new LambdaQueryWrapper<CnVirtuallyUserHwAccount>()
                   .eq(CnVirtuallyUserHwAccount::getUserName, cnVirtuallyUserHwAccount.getUserName()));
            if(userNameCount > 0){
                throw new ServiceException("用户账号已存在!");
            }
        }

        cnVirtuallyUserHwAccount.setUpdateBy(SecurityUtils.getUserId().toString());
        cnVirtuallyUserHwAccount.setUpdateTime(DateUtils.getNowDate());
        return updateById(cnVirtuallyUserHwAccount);
    }

    /**
     * 批量删除用户和华为云账号绑定（虚拟）
     *
     * @param userIds 需要删除的用户和华为云账号绑定（虚拟）主键
     * @return 结果
     */
    @Override
    public boolean deleteCnVirtuallyUserHwAccountByUserIds(List<String> userIds) {
        return update(new LambdaUpdateWrapper<CnVirtuallyUserHwAccount>()
                .in(CnVirtuallyUserHwAccount::getUserId, userIds)
                .set(CnVirtuallyUserHwAccount::getDelFlag, 0));
    }

    /**
     * 删除用户和华为云账号绑定（虚拟）信息
     *
     * @param userId 用户和华为云账号绑定（虚拟）主键
     * @return 结果
     */
    @Transactional
    @Override
    public boolean deleteCnVirtuallyUserHwAccountByUserId(String userId) {
      return update(new LambdaUpdateWrapper<CnVirtuallyUserHwAccount>()
              .eq(CnVirtuallyUserHwAccount::getUserId, userId)
              .set(CnVirtuallyUserHwAccount::getDelFlag, 0));
    }

}
